<?php


namespace App\Dao;

use App\Exceptions\SysExceptions;
use Illuminate\Database\Eloquent\Model;

/**
 * Class BaseDao
 * @package App\Dao
 * author:admin
 * desc:基础Dao层
 * created_at: 2020/10/16{
 */
class BaseDao
{
    /**
     * author:admin
     * desc:错误
     * created_at: 2020/10/16{
     * @param string $msg
     * @param int $code
     * @throws SysExceptions
     */
    public static function error($msg = '',$code = 301){
        throw new SysExceptions($msg,$code);
    }


    public static function deleteParamId(&$params, string $key="id"){
        $value = $params[$key];
        unset($params[$key]);
        return $value;
    }



    /**
     * @param $data
     * @param Model $model
     * @return array
     * author:admin
     * desc:过滤字段
     * created_at: 2020/7/22-10:17 上午
     */
    public static function filterFields($data, Model $model): array
    {
        $fields = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
        $tableColumn = array_keys($data);
        $columns= array_intersect($tableColumn, $fields);
        $return = [];
        foreach ($columns as $v){
            if(isset($data[$v])){
                $return[$v] = $data[$v];
            }
        }
        return $return;
    }
}
